﻿using LitJson;
using Sliver.Network.WebSocket;

namespace Sliver.Network.Components
{
    public class MessageOut
    {
        public int id;

        public string type;

        public string path;

        public object data;

        public MessageOut(int id, string path, object o)
        {
            this.id = id;
            type = WebSocketMessageType.Info;
            this.path = path;
            data = o;
        }

        public MessageOut(int id, string type, string path, object o)
        {
            this.id = id;
            this.type = type;
            this.path = path;
            data = o;
        }

        public string ToJson()
        {
            return JsonMapper.ToJson(this);
        }
    }

    public class MessageIn
    {
        public int id;

        public string type;

        public string path;

        public JsonData data;

        public T GetObject<T>()
        {
            return JsonMapper.ToObject<T>(data.ToJson());
        }

        public string GetString()
        {
            if (data == null) return "";
            return data.ToJson().Replace("\"", "");
        }

        public int ToInt()
        {
            return int.Parse(data.ToJson());
        }

        public MessageOut ToOut()
        {
            return new MessageOut(id, type, path, GetString());
        }

        public override string ToString()
        {
            return JsonMapper.ToJson(this);
        }
    }
}
